package com.company;

import java.util.Scanner;

public class Calculator {
    public static void main(String[] args) {
        while (true) {
            System.out.println("要进行哪种运算？\n"
                    + "1.加法\n"
                    + "2.减法\n"
                    + "3.乘法\n"
                    + "4.除法\n"
                    + "5.取余\n"
                    + "6.退出");
            int input = scanInt("请输入:");
            System.out.println("输入的内容是:" + input);
            switch (input) {
                case 1:
                    // 加法
                    System.out.println("加法流程");
                    int first1 = scanInt("请输入第一个操作数：");
                    int second1 = scanInt("请输入第二个操作数：");
                    System.out.println(first1 + "+" + second1 + "=" + (first1+second1));
                    break;
                case 2:
                    // 减法
                    System.out.println("减法流程");
                    int first2 = scanInt("请输入第一个操作数：");
                    int second2 = scanInt("请输入第二个操作数：");
                    System.out.println(first2 + "-" + second2 + "=" + (first2-second2));
                    break;
                case 3:
                    // 乘法
                    System.out.println("乘法流程");
                    int first3 = scanInt("请输入第一个操作数：");
                    int second3 = scanInt("请输入第二个操作数：");
                    System.out.println(first3 + "*" + second3 + "=" + (first3*second3));
                    break;
                case 4:
                    // 除法
                    System.out.println("除法流程");
                    int first4 = scanInt("请输入第一个操作数：");
                    int second4 = scanInt("请输入第二个操作数：");
                    System.out.println(first4 + "/" + second4 + "=" + (first4/second4));
                    break;
                case 5:
                    // 除法
                    System.out.println("取余流程");
                    int first5 = scanInt("请输入第一个操作数：");
                    int second5 = scanInt("请输入第二个操作数：");
                    System.out.println(first5 + "%" + second5 + "=" + (first5%second5));
                    break;
                case 6:
                    // 退出
                    System.out.println("程序退出，谢谢使用!");
                    return;
                default:
                    System.out.println("输入不合法，请重新输入!");
                    break;
            }
        }
    }

    public static int scanInt(String msg) {
        System.out.println(msg);
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        try {
            return Integer.parseInt(s);
        } catch (NumberFormatException e) {
            System.out.println("输入不合法，请重新输入！");
            return scanInt(msg);
        }
    }
}
